﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace GnToolControls.Convert
{
    public class FileConvert
    {
		public static int GetFileList( String findPFolder, String srcFilePath, String copyPath,
			ref List<ConvertItem> outSourceList )
		{
			if( findPFolder.Length <= 0 )
				return 0;

			String dir = Path.GetDirectoryName( srcFilePath );
			if( dir.Length <= 0 )
				return 0;

			String fileName = Path.GetFileName( srcFilePath );

			// Only get files that begin with the letter "c."
			string[] files = Directory.GetFiles( dir, fileName, SearchOption.AllDirectories );
			foreach( string file in files )
			{
				ConvertFileItem src = new ConvertFileItem();
				src.SrcPath = file;
				src.DestPath = copyPath;
			}
			return outSourceList.Count;
		}

        public bool Copy(String srcFullFilePath, String copyPath)
        {
            if (File.Exists(srcFullFilePath) == false)
                return false;

            String copyFilePath = GetCopyPath(srcFullFilePath, copyPath);
            if (Directory.Exists(copyFilePath) == false)
            {
                DirectoryInfo dirInfo = Directory.CreateDirectory(copyFilePath);
                if (dirInfo.Exists == false)
                    return false;
            }

            if (IsInvalidPath(copyPath) == false)
                return false;

            File.Copy(srcFullFilePath, copyFilePath);
            return true;
        }

        private String GetCopyPath(String srcPath, String destPath)
        {
            if (destPath.Contains(@"..\") == false && destPath.Contains(@".\") == false)
            {
                if (destPath.Contains(@"\") == false)
                {
                    return GetOnlyPath(srcPath);
                }
                else
                {
                    return GetOnlyPath(destPath);
                }
            }

            String copyPath = GetOnlyPath(srcPath);
            try
            {
                while (destPath.Contains(@"..\"))
                {
                    destPath.Remove(0, 3);
                    copyPath = System.IO.Path.GetDirectoryName(copyPath);
                }
            }
            catch
            {
            }
            return copyPath;
        }

		private String GetOnlyPath( String filePath )
        {
            String onlyPath = Path.GetFileName(filePath);
            if (onlyPath.Length > 0)
                onlyPath = Path.GetDirectoryName(filePath);
            else
                onlyPath = filePath;

            return onlyPath;
        }

		private bool IsInvalidPath( String path )
        {
            try
            {
                char[] invalidPathChars = System.IO.Path.GetInvalidFileNameChars();
                foreach (char someChar in invalidPathChars)
                {
                    if (path.Contains(someChar))
                        return false;
                }
            }
            catch
            {
            }
            return true;
        }
    }
}
